package com.derbysoft.dswitch.adapter.ihg.common.utils;

/**
 * Created by: jason
 * Date: 2012-08-29
 */
public abstract class ErrorMessageUtils {
    private static final int STACK_DEEP = 20;

    public static String buildErrorMessage(Throwable ex) {
        StringBuilder builder = new StringBuilder();
        builder.append(ex.getClass().getName()).append(":").append(ex.getMessage() == null ? "" : ex.getMessage()).append("\n");
        int levelCount = 0;
        for (StackTraceElement stackTraceElement : ex.getStackTrace()) {
            levelCount++;
            if (levelCount > STACK_DEEP) {
                break;
            }
            builder.append(stackTraceElement.getClassName() == null ? "" : stackTraceElement.getClassName())
                    .append(":").append(stackTraceElement.getMethodName() == null ? "" : stackTraceElement.getMethodName())
                    .append(":").append(stackTraceElement.getLineNumber()).append("\n");
        }
        return builder.toString();
    }
}
